package com.siwei.mes.controller.materials;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.system.SupplierCompany;
import com.siwei.mes.service.system.SupplierCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 供应商相关
 *
 * @author linzi
 * @since 2024-07-16 16:40:40
 */
@Api(value = "供应商")
@RestController
@RequestMapping(value = {"supplierCompnay", "supplierCompany"})
public class SupplierCompnayController {
    @Resource
    private SupplierCompanyService supplierCompanyService;

    @ApiOperation(value = "新增供应商  这里没有入口了")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) SupplierCompany entity) {
        return Json.succ().data(ApiResult.addColumn("id", supplierCompanyService.create(entity)));
    }

    @ApiOperation(value = "修改供应商")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) SupplierCompany entity) {
        supplierCompanyService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "禁用/启用   这里作废了")
    @PostMapping("/updateStatus")
    public Json updateStatus(@RequestBody SupplierCompany entity) {
        assertTrue(entity.getId() == null, "供应商主键id为空");
        assertTrue(entity.getStatus() == null, "状态为空");
        supplierCompanyService.updateStatus(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除供应商  这里作废了")
    @PostMapping("/delete")
    public Json delete(@RequestBody SupplierCompany entity) {
        supplierCompanyService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取供应商")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<SupplierCompany, SupplierCompany> query) {
        return Json.succ().data(supplierCompanyService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取供应商")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(supplierCompanyService.loadByYCId(id));
    }

    @ApiOperation(value = "获取所有供应商")
    @PostMapping("getAll")
    public Json getAll(@RequestBody SupplierCompany entity) {
        return Json.succ().data(ApiResult.addList(supplierCompanyService.getAll(entity)));
    }

    @ApiOperation(value = "根据材料名称和规格获取对应材料所有的供应商信息")
    @GetMapping("selectBySupplierCompanyList")
    public Json selectBySupplierCompanyList(@RequestParam("materialsName") String materialsName, @RequestParam("materialsSpec") String materialsSpec) {
        return Json.succ().data(ApiResult.addList(supplierCompanyService.selectBySupplierCompanyList(materialsName, materialsSpec)));
    }

    @ApiOperation(value = "根据物料名称和物料规格获取所有的供应商信息")
    @GetMapping("selectByMaterialSupplierCompanyList")
    public Json selectByMaterialSupplierCompanyList(@RequestParam("materialsType") Integer materialsType,
            @RequestParam("materialsName") String materialsName, @RequestParam("materialsSpec") String materialsSpec) {
        return Json.succ().data(ApiResult.addList(supplierCompanyService.selectByMaterialSupplierCompanyList(materialsType,materialsName, materialsSpec)));
    }

    @ApiOperation(value = "根据物料类型所有的供应商信息")
    @GetMapping("selectBySupplierCompanyListByType")
    public Json selectBySupplierCompanyListByType(@RequestParam("materialsType") Integer materialsType,
                                                  @RequestParam(value = "materialsName",required = false) String materialsName) {
        return Json.succ().data(ApiResult.addList(supplierCompanyService.selectBySupplierCompanyListByType(materialsType,materialsName)));
    }
}
